#include "ASin.h"
long double ASin::calcul(long double aux1)
{
    if (aux1 < -1 || aux1 > 1)
    {
        std::cout<<"Arcsin e definit intre [-1..1]";
        int sgn = aux1 < 0 ? -1 : 1;

        while (sgn * aux1 > 1)
            aux1 -= sgn * 2;
    }
    return asinl(aux1);
}

long double ASin::calcul()
{
    if (!fStang)
        exit(1);
    return calcul(fStang->calcul());
}

char *ASin::derivare()
{
    char *aux1, *aux2, *aux, *aux00, *aux01, *aux02;

    if (!fStang)
        exit(1);

    aux1 = fStang->derivare();
    aux2 = fStang->toString();

    aux00 = Putere::concatTermeni(aux2, "2");
    aux01 = Minus::concatTermeni("1", aux00);
    aux02 = Putere::concatTermeni(aux01, "0.5");
    aux = Impartit::concatTermeni(aux1, aux02);
    delete aux1; delete aux2; delete aux00; delete aux01; delete aux02;

    return aux;
}

long double ASin::calculDerivata()
{
    if (!fStang)
        exit(1);

    return Impartit::calcul(fStang->calculDerivata(), Putere::calcul(
            Minus::calcul(1, Putere::calcul(fStang->calcul(), 2)), 0.5));
}

char *ASin::toString()
{
    char *aux, *aux1;
    int nrP;

    if (!fStang)
        exit(1);
    aux1 = fStang->toString();

    aux = concatTermeni(aux1);
    delete aux1;
    return aux;
}

char *ASin::concatTermeni(char *aux1)
{
    char *aux;
    int nrP;

    nrP = aux1[0] == '(' ? 0 : 2;
    aux = new char[strlen(aux1) + 5 + nrP];

    strcpy(aux, "asin");
    if (nrP) strcat(aux, "(");
    strcat(aux, aux1);
    if (nrP) strcat(aux, ")");

    return aux;
}
